package exercises;

/**
 * @author zerorain
 **/
import java.util.*;
//数组中出现超过一半的数字  简单的map问题

public class ex5 {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length == 0){
            return 0;
        }

        int len = array.length;
        int threshold = len/2;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < len; i++){
            if(map.get(array[i]) == null){
                map.put(array[i],1);
            }else{
                map.put(array[i],map.get(array[i])+1);
            }
        }

        for(int j = 0; j < array.length ; j++ ){
            if(map.get(array[j]) > threshold){
                return array[j];
            }
        }

        return 0;
    }
}